package com.baiwei.vpnapi.controller;

import com.baiwei.vpnapi.entity.reqentity.PageInput;
import com.baiwei.vpnapi.entity.reqentity.PushInput;
import com.baiwei.vpnapi.service.impl.AppServiceImpl;
import com.baiwei.vpnapi.service.impl.PushServiceImpl;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: vpnapi
 * @description: app业务控制器
 * @author: Mr. Stone
 * @create: 2020-03-06 14:42
 **/
@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    private PushServiceImpl pushService;

    @Autowired
    private AppServiceImpl appService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/pushid/{id}")
    public Object pushById(@PathVariable("id") Integer id) {
        String adminToken = request.getHeader("token");
        String redisToken = stringRedisTemplate.opsForValue().get("adminToken");
        System.out.println("redis token: " + redisToken);
        if (adminToken == null || adminToken.equals("")) {
            throw new RuntimeException("Token empty");
        }

        if (!redisToken.equals(adminToken)) {
            throw new RuntimeException("Invalid token");
        }
        return ResultVoUtils.success(pushService.pushById(id));
    }

    // 根据id获取推送消息
    @GetMapping("/message/{id}")
    public Object getMessageById(@PathVariable("id") Integer id) {

        return ResultVoUtils.success(pushService.messageById(id));
    }

    // 获取广告
    @GetMapping("/advert")
    public Object getAdvert() {
        return ResultVoUtils.success(appService.getAdvertisements());
    }

    @PostMapping("/push")
    public Object push(@RequestBody PushInput input) {
        input.validation();
        String title = input.getTitle();
        String content = input.getContent();
        if (input.getPlatform().equals("ios")) {
            return ResultVoUtils.success(pushService.iosPushNotify(title, content, null));
        } else if (input.getPlatform().equals("andriod")) {
            return ResultVoUtils.success(pushService.androidPushNotify(title, content, null));
        } else {
            return ResultVoUtils.success(pushService.pushNofity(title, content, null));
        }
    }

    @GetMapping("/macos")
    public Object macOnlineConfig() {
        return ResultVoUtils.success(appService.macOnline());
    }

    @GetMapping("/config")
    public Object getConfig() {
        return ResultVoUtils.success(appService.getAppConfig());
    }

    @PostMapping("/messages")
    public Object getMessage(@RequestBody PageInput input) {
        input.validation();
        return ResultVoUtils.success(appService.getMessage(input.getPageNo(), input.getPageSize()));
    }

    @PostMapping("/uploadToken")
    public Object uploadToken(@RequestBody Map<String, String> info) {
        String token = info.get("token");
        if (token == null || token.length() == 0) {
            throw new RuntimeException("device token empty");
        }
        return ResultVoUtils.success(appService.uploadIosToken(token));
    }
}
